#!/usr/bin/env bash
Xrealfile=$(realpath "$0")
PrefixDir=$(dirname "$Xrealfile")
cd "$PrefixDir" || exit
__Author__="Force.Charlie"
__Date__="2018.04.09"

echo "Launcher script author: $__Author__ date:$__Date__"

GitStatusMessage=$(git status 2>/dev/null)
if [[ "$GitStatusMessage" != "" ]]; then
	MyCommit=$(git rev-parse HEAD)
	Branch=$(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')
	Message=$(git rev-list --pretty=email --max-count=1 "$MyCommit")
	echo -e "\\x1b[33msserver binary last update info $Branch:\\n$Message\\x1b[0m\\n"
fi

function usage() {
	cat <<EOF
sserver launcher helper
usage: launcher [options]
       -start          start sserver
       -stop           stop sserver
       -status         check sserver run status
       -restart        restart sserver
       -help           Print sserver launcher help
EOF
	exit 0
}

function Appstopimpl() {
	if [ -f "$PrefixDir/launcher.lock.pid" ]; then
		echo "Found pid lock file launcher.lock.pid, will kill sserver !"
	else
		echo -e "\\x1b[31mNot found launcher.lock.pid in $PrefixDir \\xE2\\x9D\\x8C\\x1b[0m"
		return 1
	fi
	pid=$(cat "$PrefixDir/launcher.lock.pid")
	if [ ! -d "/proc/$pid" ]; then
		echo "server not running, pid:$pid"
		rm "$PrefixDir/launcher.lock.pid"
		return 1
	fi
	if ! kill -9 "$pid"; then
		echo -e "\\x1b[31mKill server process failed, pid is $pid \\xE2\\x9D\\x8C\\x1b[0m"
	else
		echo -e "\\x1b[32mKill server process success, pid is $pid \\xE2\\x9C\\x94\\x1b[0m"
		rm "$PrefixDir/launcher.lock.pid"
		echo -e "\\x1b[32mRemove mapdb cache \\xE2\\x9C\\x94\\x1b[0m"
		rm -rf "$PrefixDir/caches"
		return 0
	fi
	rm "$PrefixDir/launcher.lock.pid"
	return 1
}

function Appstartimpl() {
	if [ -f "$PrefixDir/launcher.lock.pid" ]; then
		pid=$(cat "$PrefixDir/launcher.lock.pid")
		if [ -d "/proc/$pid" ]; then
			echo -e "\\x1b[31mold process is running, pid is $pid \\xE2\\x9D\\x8C\\x1b[0m"
			return 1
		fi
	fi

	if [ -f "$PrefixDir/launcher.profile.bash" ]; then
		# shellcheck disable=SC1090
		. "$PrefixDir/launcher.profile.bash"
	fi

	if [ -z "$Appstdout" ]; then
		echo "Appstdout not exist"
		Appstdout="/tmp/appstdout.error.log"
	fi
	if [ -z "$Appname" ]; then
		Appname="sserver.svnserve.bridge.jar"
	fi
	if [ -z "$Appjavahome" ]; then
		Appjavahome=""
	fi
	if [ -z "$Appcpulimit" ]; then
		Appcpulimit=50
	fi
	javabin=$(command -v java)
	if [ -f "$Appjavahome/bin/java" ]; then
		javabin="$Appjavahome/bin/java"
	fi

	GREP=$(command -v egrep)
	CAT=$(command -v cat)
	VM_OPTIONS_FILE="$PrefixDir/launcher.vmoptions"
	Xvmoptions=""
	if [ -r "$VM_OPTIONS_FILE" ]; then
		Xvmoptions=$("$CAT" "$VM_OPTIONS_FILE" | "$GREP" -v "^#.*")
	fi
	
	# shellcheck disable=SC2086
	nohup "$javabin" "-Dlog4j.configurationFile=$PrefixDir/log4j.xml" ${Xvmoptions} -jar "$Appname" >"$Appstdout" &
	javaId=$!
	if [ -d "/proc/$javaId" ]; then
		echo $! >"$PrefixDir/launcher.lock.pid"
		echo -e "\\x1b[32mStart sserver success, pid: $javaId \\xE2\\x9C\\x94\\x1b[0m"
		echo "Your can lookup logs,log file path configured by log4j.xml"
		if hash cpulimit 2>/dev/null; then
			nohup cpulimit -p "$javaId" -l $Appcpulimit >"/tmp/subversion.cuplimit.log" 2>&1 &
		fi
		echo -e "Set process $javaId cpulimit: \\x1b[32m$Appcpulimit%\\x1b[0m"
	else
		echo "\\x1b[31mStart $Appname failed ,your should check $Appstdout and base logs (configured by log4j.xml) \\xE2\\x9D\\x8C\\x1b[0m"
	fi
}

function Appstart() {
	echo "launcher script will start sserver."
	Appstartimpl
	exit 0
}
###
function Appstop() {
	echo "launcher script will stop sserver"
	Appstopimpl
	exit 0
}
##
function Apprestart() {
	echo "launcher script will restart sserver."
	Appstopimpl
	if [ $? -eq "0" ]; then
		Appstartimpl
	else
		echo -e "\\x1b[31m\\xE2\\x9D\\x8C sserver stop failed\\x1b[0m"
	fi
	exit 0
}

function Appstatus() {
	echo "checking sserver status ..."
	if [ ! -f "$PrefixDir/launcher.lock.pid" ]; then
		echo -e "\\x1b[31mNot found launcher.lock.pid in $PrefixDir \\xE2\\x9D\\x8C\\x1b[0m"
		exit 1
	fi
	pid=$(cat "$PrefixDir/launcher.lock.pid")
	if [ ! -d "/proc/$pid" ]; then
		echo "sserver not running,pid:$pid"
		rm "$PrefixDir/launcher.lock.pid"
		exit 1
	else
		echo "sserver start time:"
		ps -p "$pid" -o lstart
		echo "sserver is running,status info>"
		cat "/proc/$pid/status"
	fi
	exit 0
}

for arg; do
	case "$arg" in
	-help) usage ;;
	-start) Appstart ;;
	-stop) Appstop ;;
	-status) Appstatus ;;
	-restart) Apprestart ;;
	esac
done

usage
